<?php

namespace Models;

use Models\Base;
use Models\User;

class Profile extends Base {
    const PROVIDER_FB = 1;
    const PROVIDER_GPLUS = 2;
    const PROVIDER_DRAUGIEM = 3;

    protected $id;
    protected $provider;
    protected $user_id;
    protected $access_token;
    protected $data;

    protected $user;

    public function getId() {
        return $this->id;
    }

    /**
     *
     * @return \Models\User
     */
    public function generateUserData() {
        $data = array(
            'name' => $this->data['name'],
            'first_name' => $this->data['first_name'],
            'last_name' => $this->data['last_name'],
            'gender' => $this->data['sex'],
            'timezone' => $this->data['timezone'],
            'status_id' => User::ACTIVE,
            'username' => $this->data['username'],
            'email' => $this->data['email'],
        );
        $user = new User($data);

        return $user;
    }

    /**
     *
     * @param User $user
     */
    public function setUser(User $user) {
        $this->user_id = $user->getId();
        $this->user = $user;
    }

    /**
     * (non-PHPdoc)
     * @see Models.Base::fromArray()
     */
    public function fromArray(array $data) {
        if (isset($data['data']) && is_string($data['data'])) {
            $data['data'] = json_decode($data['data'], true);
        }

        return parent::fromArray($data);
    }

    /**
     * (non-PHPdoc)
     * @see Models.Base::toArray()
     */
    public function toArray() {
        return array_merge(parent::toArray(), array(
            'id' => $this->id,
            'provider' => $this->provider,
            'user_id' => $this->user_id,
            'access_token' => $this->access_token,
            'data' => json_encode($this->data)
        ));
    }

    public function getData($k = null) {
        return $k ? $this->data[$k] : $this->data;
    }
}